//
//  SmallDecisionsDpSetViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/25.
//

#import "SmallDecisionsDpSetViewController.h"
#import "SmallDecisionsDpViewController.h"
#import "SmallDecisionsTextField.h"
#import "SmallDecisionsSlider.h"
#import "SmallDecisionsDpColorsView.h"

@interface SmallDecisionsDpSetViewController ()

@property (nonatomic, strong) UIScrollView *dpScrollView;
@property (nonatomic, strong) SmallDecisionsTextField *dpWordTextField;
@property (nonatomic, strong) SmallDecisionsSlider *dpWordFontSizeSliderView;
@property (nonatomic, strong) SmallDecisionsSlider *dpWordFontSpeedSliderView;
@property (nonatomic, strong) UIColor *dpWordFontColor;
@property (nonatomic, strong) UIColor *dpWordBgColor;

@end

@implementation SmallDecisionsDpSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"手持灯牌";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.dpWordFontColor = [UIColor redColor];
    self.dpWordBgColor = [UIColor blackColor];
    [self createTeamProjectDanMuSetVCSubviews];
}

- (void)createTeamProjectDanMuSetVCSubviews {
    SmallDecisionsTextField *dpWordTextField = [self createGlobalTextFieldWithTitle:@"弹幕内容" placeholder:@"请输入弹幕内容"];
    [self.dpScrollView addSubview:dpWordTextField];
    [dpWordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(20);
        make.trailing.equalTo(self.view).offset(-20);
        make.top.mas_equalTo(20);
        make.height.mas_equalTo(50);
    }];
    self.dpWordTextField = dpWordTextField;


    [self.dpWordFontSizeSliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(dpWordTextField.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(self.view).mas_offset(20);
        make.right.mas_equalTo(self.view).mas_offset(-20);
        make.height.mas_equalTo(50);
    }];

    [self.dpWordFontSpeedSliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.dpWordFontSizeSliderView.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(self.view).mas_offset(20);
        make.right.mas_equalTo(self.view).mas_offset(-20);
        make.height.mas_equalTo(50);
    }];

    UILabel *dpFontTipLable = [self createDpDefaultLableWithText:@"字体颜色"];
    [self.dpScrollView addSubview:dpFontTipLable];
    [dpFontTipLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(dpWordTextField);
        make.top.equalTo(self.dpWordFontSpeedSliderView.mas_bottom).offset(20);
    }];
    
    SmallDecisionsDpColorsView *dpWordFontColorView = [[SmallDecisionsDpColorsView alloc] init];
    @weakify(self)
    dpWordFontColorView.selectFinishBlock = ^(UIColor *color) {
        @strongify(self)
        self.dpWordFontColor = color;
    };
    [self.dpScrollView addSubview:dpWordFontColorView];
    [dpWordFontColorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(dpWordTextField);
        make.top.equalTo(dpFontTipLable.mas_bottom).offset(10);
        make.height.mas_equalTo(100);
    }];

    UILabel *dpBgTipLable = [self createDpDefaultLableWithText:@"背景颜色"];
    [self.dpScrollView addSubview:dpBgTipLable];
    [dpBgTipLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(dpFontTipLable);
        make.top.equalTo(dpWordFontColorView.mas_bottom);
    }];
    
    SmallDecisionsDpColorsView *dpWordBgColorView = [[SmallDecisionsDpColorsView alloc] init];
    dpWordBgColorView.colorList = [NSArray arrayWithObjects:[UIColor blackColor],[UIColor orangeColor],[UIColor whiteColor],[UIColor greenColor],[UIColor grayColor],[UIColor redColor],nil];
    dpWordBgColorView.selectFinishBlock = ^(UIColor *color) {
        @strongify(self)
        self.dpWordBgColor = color;
    };
    [self.dpScrollView addSubview:dpWordBgColorView];
    [dpWordBgColorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(dpWordFontColorView);
        make.top.equalTo(dpBgTipLable.mas_bottom).offset(10);
        make.height.mas_equalTo(100);
    }];

    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureBtn setTitle:@"显示弹幕" forState:UIControlStateNormal];
    [sureBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
    [sureBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateHighlighted];
    sureBtn.titleLabel.font = SmallDecisionsSetFontSize(16);
    sureBtn.layer.cornerRadius = 22;
    sureBtn.layer.masksToBounds = YES;
    sureBtn.backgroundColor = SmallDecisionsThemeColor;
    [sureBtn addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.dpScrollView addSubview:sureBtn];

    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dpWordBgColorView.mas_bottom).offset(30);
        make.leading.trailing.equalTo(dpWordBgColorView);
        make.height.mas_equalTo(50);
        make.bottom.mas_equalTo(-30);
    }];
}

- (void)sureBtnAction {
    NSString *text = self.dpWordTextField.text;
    if(text.length) {
        SmallDecisionsDpViewController *dpVC = [[SmallDecisionsDpViewController alloc] init];
        dpVC.dpText = self.dpWordTextField.text;
        dpVC.dpFontColor = self.dpWordFontColor;
        dpVC.dpBgColor = self.dpWordBgColor;
        dpVC.dpFontSize = self.dpWordFontSizeSliderView.slider.value;
        dpVC.dpSpeed = self.dpWordFontSpeedSliderView.slider.value;
        dpVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:dpVC animated:YES completion:nil];
    } else {
        [SmallDecisionsHUDHelper showError:@"请输入弹幕现实内容"];
    }

}

- (UILabel *)createDpDefaultLableWithText:(NSString *)text {
    UILabel *dpLabel = [[UILabel alloc] init];
    dpLabel.textColor = SmallDecisionsThemeFontColor;
    dpLabel.font = SmallDecisionsSetFontSize(16);
    dpLabel.text = text;
    return dpLabel;
}

- (SmallDecisionsTextField *)createGlobalTextFieldWithTitle:(NSString *)title
                                                placeholder:(NSString *)placeholder{

    SmallDecisionsTextField *textField = [[SmallDecisionsTextField alloc] init];
    textField.leadingViewRect = CGRectMake(0, 0, 80, 50);
    textField.trailingViewRect = CGRectMake(0, 0, 10, 50);
    [textField.leadingViewBtn setTitle:title forState:UIControlStateNormal];
    textField.leadingViewBtn.titleLabel.font = SmallDecisionsSetFontSize(16);
    textField.leadingViewBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [textField.leadingViewBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
    textField.textAlignment = 2;
    textField.font = SmallDecisionsSetFontSize(16);
    textField.placeholderColor = [UIColor colorWithHexString:@"0x999999"];
    textField.placeholder = placeholder;
    textField.lineView.hidden = NO;
    return textField;
}

- (UIScrollView *)dpScrollView {
    if(!_dpScrollView) {
        _dpScrollView = [[UIScrollView alloc] init];
        _dpScrollView.alwaysBounceVertical = YES;
        [self.view addSubview:_dpScrollView];
        [_dpScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.right.bottom.mas_equalTo(0);
        }];
    }
    return _dpScrollView;
}

- (SmallDecisionsSlider *)dpWordFontSizeSliderView {
    if(!_dpWordFontSizeSliderView) {
        _dpWordFontSizeSliderView = [[SmallDecisionsSlider alloc] initWithMinValueFormat:nil maxValueFormat:^NSString * _Nonnull(UISlider * _Nonnull slider) {
            return [NSString stringWithFormat:@"%.0f", slider.value];
        }];
        _dpWordFontSizeSliderView.layer.cornerRadius = 8;
        _dpWordFontSizeSliderView.layer.masksToBounds = YES;
        _dpWordFontSizeSliderView.titleLabel.text = @"字体大小";
        _dpWordFontSizeSliderView.titleLabel.font = SmallDecisionsSetFontSize(16);
        _dpWordFontSizeSliderView.minimumValue = 20;
        _dpWordFontSizeSliderView.maximumValue = 200;
        _dpWordFontSizeSliderView.value = 100;
        [self.dpScrollView addSubview:_dpWordFontSizeSliderView];
    }
    return _dpWordFontSizeSliderView;
}



- (SmallDecisionsSlider *)dpWordFontSpeedSliderView {
    if(!_dpWordFontSpeedSliderView) {
        _dpWordFontSpeedSliderView = [[SmallDecisionsSlider alloc] initWithMinValueFormat:nil maxValueFormat:^NSString * _Nonnull(UISlider * _Nonnull slider) {
            return [NSString stringWithFormat:@"%.0f", slider.value];
        }];
        _dpWordFontSpeedSliderView.titleLabel.text = @"弹幕速度";
        _dpWordFontSpeedSliderView.titleLabel.font = SmallDecisionsSetFontSize(16);
        _dpWordFontSpeedSliderView.minimumValue = 50;
        _dpWordFontSpeedSliderView.maximumValue = 500;
        _dpWordFontSpeedSliderView.value = 200;
        [self.dpScrollView addSubview:_dpWordFontSpeedSliderView];
    }
    return _dpWordFontSpeedSliderView;
}

@end
